﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BrightcoveAPI
{
    // <summary>
    // The base class for all Brightcove API Objects
    // </summary>     
    public class BCObject
    {
        #region Date/Time Conversion

        // <summary>
        // Converts a UNIX timestamp to a .NET DateTime
        // </summary>         
        protected DateTime DateFromUnix(object value)
        {
            long millisecs = long.Parse(value.ToString());
            double secs = millisecs / 1000;
            return new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(secs);
        }

        // <summary>
        // Converts a .NET DateTime to a UNIX timestamp
        // </summary>         
        protected long DateToUnix(DateTime value)
        {
            TimeSpan ts = value - new DateTime(1970, 1, 1, 0, 0, 0);
            return ts.Milliseconds;
        }

        #endregion Date/Time Conversion

        #region Implode/Explode
        // <summary>
        // Implodes a List<> to a comma-separated string
        // </summary>  
        protected String Implode(List<String> values)
        {
            String result = "";
            int i = 1;
            foreach (String s in values)
            {
                result = result + s;
                if (i < values.Count)
                {
                    result = result + ",";
                }
                i++;
            }
            return result;
        }

        // <summary>
        // Implodes a List<> to a comma-separated string
        // </summary>  
        protected String Implode(List<long> values)
        {
            String result = "";
            int i = 1;
            foreach (long l in values)
            {
                result = result + l.ToString();
                if (i < values.Count)
                {
                    result = result + ",";
                }
            }
            return result;
        }

        // <summary>
        // Explodes a comma-separated string to a List of strings
        // </summary>  
        protected List<String> ExplodeString(String values)
        {
            List<String> result = new List<string>();
            String[] vsplit = values.Split(',');
            foreach (String s in vsplit)
            {
                result.Add(s.Trim());
            }
            return result;
        }

        // <summary>
        // Explodes a comma-separated string to a List of longs
        // </summary>  
        protected List<long> ExplodeLong(String values)
        {
            List<long> result = new List<long>();
            String[] vsplit = values.Split(',');
            foreach (String s in vsplit)
            {
                result.Add(long.Parse(s));
            }
            return result;
        }

        #endregion Implode/Explode
    }
}
